﻿Imports System.Data
Imports System.Data.SqlClient
Imports Entidad

Public Class NotaIngresoDetalleDatos

    Sub registrarNotaIngresoDetalle(ByVal nid As NotaIngresoDetalle)
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "INSERT INTO Documento_Almacen_Detalle(CIA,SUC,TDOC_Codigo,DCA_Codigo,DAD_Codigo,ART_Codigo,DAD_Cantidad) " &
                              "VALUES(@cia,@suc,@tdoc,@dcaCodigo,@dadcodigo,@artcodigo,@dadCantidad)"
        Dim comando As New SqlCommand
        With comando
            .Connection = coneccion
            .CommandText = query
            .Parameters.AddWithValue("@cia", nid.CIA)
            .Parameters.AddWithValue("@suc", nid.SUC)
            .Parameters.AddWithValue("@tdoc", nid.TDOC_Codigo)
            .Parameters.AddWithValue("@dcaCodigo", nid.DCA_Codigo)
            .Parameters.AddWithValue("@dadcodigo", nid.DAD_Codigo)
            .Parameters.AddWithValue("@artcodigo", nid.ART_Codigo)
            .Parameters.AddWithValue("@dadCantidad", nid.DAD_Cantidad)
            .ExecuteNonQuery()
        End With
        coneccion.Close()
    End Sub

    Function obtenerCodigoAlmacenDetalle(ByVal codNota As String, ByVal tdoc As String) As String
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "Select DAD_Codigo from Documento_Almacen_Detalle where " &
                              "TDOC_Codigo = '" & tdoc & "' and DCA_Codigo = '" & codNota & "'"
        Dim adapter As New SqlDataAdapter(query, coneccion)
        Dim ds As New DataSet
        adapter.Fill(ds)

        coneccion.Close()

        Return ds.Tables(0).Rows(0)("DAD_Codigo").ToString.Trim
    End Function

    Function cantidadDetalleIngreso(ByVal codDoc As String, ByVal tdoc As String) As Integer
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "select count(*) as cont from Documento_Almacen_Detalle where DCA_Codigo = '" & codDoc & "' and " &
                              "TDOC_Codigo = '" & tdoc & "'"
        Dim adapter As New SqlDataAdapter(query, coneccion)
        Dim ds As New DataSet
        adapter.Fill(ds)

        coneccion.Close()

        Return CInt(ds.Tables(0).Rows(0)("cont").ToString.Trim)
    End Function

    Function obtenerCodigosDetalle(ByVal tdoc As String, ByVal codDoc As String, ByVal cia As String, ByVal suc As String) As DataSet
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "SELECT DAD_Codigo FROM Documento_Almacen_Detalle " &
                              "WHERE CIA = '" & cia & "' AND SUC = '" & suc & "' " &
                              "AND TDOC_Codigo = '" & tdoc & "' AND DCA_Codigo = '" & codDoc & "'"
        Dim adapter As New SqlDataAdapter(query, coneccion)
        Dim ds As New DataSet
        adapter.Fill(ds)

        coneccion.Close()

        Return ds
    End Function

End Class
